package jp.melodroid.no7.various_widgets;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import android.widget.EditText;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;

import android.widget.CheckBox;
import android.widget.CompoundButton;

import android.widget.RadioGroup;
import android.widget.RadioButton;

import android.widget.RatingBar;

public class VariousWidgets extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

	prepareCustomButton();
	prepareEditText();
	prepareCheckBox();
	prepareRadioButton();
	prepareToggleButton();
	prepareRatingBar();
    }


    /* Custom Buttons */
    private void prepareCustomButton() {
	/* button1 */
	Button button = (Button)findViewById(R.id.button1);
	button.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
		    Toast.makeText(VariousWidgets.this,"button1",Toast.LENGTH_SHORT).show();
		}
	    });

	/* button2 */
	button = (Button)findViewById(R.id.button2);
	button.setOnClickListener(new View.OnClickListener(){
		@Override
		public void onClick(View v) {
		    Toast.makeText(VariousWidgets.this,"button2",Toast.LENGTH_SHORT).show();
		}
	    });
    }


    /* Edit Text */
    private void prepareEditText() {
	final EditText et = (EditText)findViewById(R.id.edittext);

	et.setOnKeyListener(new OnKeyListener() {
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {

		    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
			(keyCode == KeyEvent.KEYCODE_ENTER)) {
			Toast.makeText(VariousWidgets.this,et.getText(),Toast.LENGTH_SHORT).show();
			return true;
		    }

		    /* kakenavi Trap */
		    if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_K:
			case KeyEvent.KEYCODE_V:
			    Toast.makeText(VariousWidgets.this,"kakenavi",Toast.LENGTH_SHORT).show();
			    return true;
			case KeyEvent.KEYCODE_B:
			    Toast.makeText(VariousWidgets.this,"kakenavi",Toast.LENGTH_SHORT).show();
			    return false;
			}
		    }

		    return false;
		}
	    });
    }


    /* Check Box */
    private void prepareCheckBox() {
	final CheckBox checkbox = (CheckBox)findViewById(R.id.checkbox);
	checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
		int kakenaviTrap = 0;

		@Override
		public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
		    CheckBox v = (CheckBox)buttonView;
		    if (isChecked) {
			if (kakenaviTrap++ > 1) {
			    kakenaviTrap = 0;
			    v.setChecked(false);
			    Toast.makeText(VariousWidgets.this,"kakenavi:no!",
					   Toast.LENGTH_SHORT).show();
			    return;
			}

			Toast.makeText(VariousWidgets.this,"Selected",
				       Toast.LENGTH_SHORT).show();
		    }
		    else {
			Toast.makeText(VariousWidgets.this,"Not Selected",
				       Toast.LENGTH_SHORT).show();
		    }
		}
	    });
    }


    /* Radio Buttons */
    private void prepareRadioButton() {
	//TODO
	RadioGroup rg = (RadioGroup)findViewById(R.id.radio_group);
	rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
		@Override
		public void onCheckedChanged(RadioGroup group,int checkedId) {
		    String s = "えらー";

		    switch(checkedId) {
		    case R.id.radio_red:
			s = new StringBuilder().append("えいるっち (")
			    .append(checkedId).append(")").toString();
			break;
		    case R.id.radio_green:
			s = new StringBuilder().append("ひむひむ (")
			    .append(checkedId).append(")").toString();
			break;
		    default:
			s = new StringBuilder().append("その他 (")
			    .append(checkedId).append(")").toString();
			break;
		    }
		    Toast.makeText(VariousWidgets.this,s,
				   Toast.LENGTH_SHORT).show();

		}
	    });

    }


    /* Toggle Button */
    private void prepareToggleButton() {
	//TODO
    }


    /* Rating Bar */
    private void prepareRatingBar() {
	RatingBar ratingbar = (RatingBar)findViewById(R.id.ratingbar);
	ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
		public void onRatingChanged(RatingBar b,float rating, boolean fromUser) {
		    StringBuilder s = new StringBuilder()
			.append("Now Rate is ")
			.append(rating)
			.append("/")
			.append(b.getNumStars())
			.append(" fromUser= ")
			.append(fromUser);
		    Toast.makeText(VariousWidgets.this,s.toString(),
				   Toast.LENGTH_SHORT).show();
		}
	    });
    }

}
